home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 3 / Info_Mac_1994-01.iso / Development / Source / IRC client Source / ircle sources / IRCPreferences.p < prev    next >
Encoding:
Text File  |  1993-07-16  |  8.3 KB  |  343 lines  |  [TEXT/PJMM]

  1. {    ircle - Internet Relay Chat client    }
  2. {    File: IRCPreferences    }
  3. {    Copyright © 1992 Olaf Titz (s_titz@ira.uka.de)    }
  4.  
  5. {    This program is free software; you can redistribute it and/or modify    }
  6. {    it under the terms of the GNU General Public License as published by    }
  7. {    the Free Software Foundation; either version 2 of the License, or    }
  8. {    (at your option) any later version.    }
  9.  
  10. {    This program is distributed in the hope that it will be useful,    }
  11. {    but WITHOUT ANY WARRANTY; without even the implied warranty of    }
  12. {    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    }
  13. {    GNU General Public License for more details.    }
  14.  
  15. {    You should have received a copy of the GNU General Public License    }
  16. {    along with this program; if not, write to the Free Software    }
  17. {    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.    }
  18.  
  19. unit IRCPreferences;
  20. { deals with the Preferences settings and files }
  21.  
  22. interface
  23. uses
  24.     TCPTypes, TCPStuff, TCPConnections, ApplBase, MsgWindows, IRCGlobals;
  25.  
  26. procedure InitIRCPreferences;
  27. { startup }
  28.  
  29. function GetPrefs (request: boolean): boolean;
  30. { get prefs from file (request=false)/ inquire (request=true) }
  31.  
  32. procedure GetShortcuts;
  33. { inquire new shortcuts }
  34.  
  35. implementation
  36.  
  37. {$SETC DEBUG=false}
  38.  
  39. {$IFC DISTRIBUTION }
  40. {$SETC DEBUG=FALSE }
  41. {$ENDC}
  42.  
  43. const
  44.     MAXMENU = 16;
  45.  
  46. var
  47.     shcd: DialogPtr;
  48.  
  49. function NewClearHandle (nbytes: longint): Handle;
  50. inline
  51.     $201F,        {    MOVE.L (A7)+, D0        }
  52.     $A322,        {    _NewHandle,Clear        }
  53.     $2E88,        {    MOVE.L A0,(A7)        }
  54.     $31C0, $0220;    {    MOVE.W D0,MemError    }
  55.  
  56. procedure defTODia (d: DialogPtr);
  57.     var
  58.         h: Handle;
  59.         i, j: integer;
  60.         l: longint;
  61.         s: Str255;
  62.         r: Rect;
  63.     begin
  64.         GetDItem(d, 3, i, h, r);
  65.         SetIText(h, default^^.Server);
  66.         GetDItem(d, 4, i, h, r);
  67.         NumToString(default^^.Port, s);
  68.         SetIText(h, s);
  69.         GetDItem(d, 5, i, h, r);
  70.         SetIText(h, default^^.Nick);
  71.         GetDItem(d, 6, i, h, r);
  72.         SetIText(h, default^^.userLoginName);
  73.         GetDItem(d, 7, i, h, r);
  74.         SetIText(h, default^^.userName);
  75.         GetDItem(d, 8, i, h, r);
  76.         SetIText(h, default^^.autoExec);
  77.         for i := 1 to 4 do begin
  78.             GetDItem(d, i + 8, j, h, r);
  79.             SetCtlValue(ControlHandle(h), ord(default^^.notify[i]))
  80.         end;
  81.     end;
  82.  
  83. procedure defFROMDia (d: DialogPtr);
  84.     var
  85.         h: Handle;
  86.         i, j: integer;
  87.         l: longint;
  88.         s: Str255;
  89.         r: Rect;
  90.     begin
  91.         GetDItem(d, 3, i, h, r);
  92.         GetIText(h, default^^.Server);
  93.         GetDItem(d, 4, i, h, r);
  94.         GetIText(h, s);
  95.         StringToNum(s, l);
  96.         default^^.Port := loword(l);
  97.         GetDItem(d, 5, i, h, r);
  98.         GetIText(h, default^^.Nick);
  99.         GetDItem(d, 6, i, h, r);
  100.         GetIText(h, default^^.userLoginName);
  101.         GetDItem(d, 7, i, h, r);
  102.         GetIText(h, default^^.userName);
  103.         GetDItem(d, 8, i, h, r);
  104.         GetIText(h, default^^.autoExec);
  105.         for i := 1 to 4 do begin
  106.             GetDItem(d, i + 8, j, h, r);
  107.             default^^.notify[i] := boolean(GetCtlValue(ControlHandle(h)))
  108.         end;
  109.     end;
  110.  
  111. procedure SetShortcutsMenu;
  112.     var
  113.         m: MenuHandle;
  114.         i, j: integer;
  115.         s: str255;
  116.     begin
  117.         m := GetMHandle(261);
  118.         for i := 1 to 10 do begin
  119.             if Shortcuts^^[i] = '' then
  120.                 s := ' '
  121.             else if length(Shortcuts^^[i]) > MAXMENU then
  122.                 s := concat(copy(Shortcuts^^[i], 1, MAXMENU), '...')
  123.             else
  124.                 s := Shortcuts^^[i];
  125.             j := 1;
  126.             while j <= length(s) do begin
  127.                 if s[j] = chr(13) then begin
  128.                     s[j] := '\';
  129.                     insert('n', s, j + 1)
  130.                 end;
  131.                 j := succ(j)
  132.             end;
  133.             SetItem(m, i + 2, s);
  134.         end;
  135.     end;
  136.  
  137. function CloseShortcuts (var e: eventRecord): boolean;
  138.     var
  139.         i, j: integer;
  140.         h: Handle;
  141.         r: Rect;
  142.         s: str255;
  143.     begin
  144.         if shcd <> nil then
  145.             if DialogPtr(e.message) = shcd then
  146.                 if TrackGoAway(shcd, e.where) then begin
  147.                     for i := 1 to 10 do begin
  148.                         GetDItem(shcd, i, j, h, r);
  149.                         GetIText(h, s);
  150.                         if s = ' ' then
  151.                             s := ''
  152.                         else begin
  153.                             j := 1;
  154.                             while j < length(s) do begin
  155.                                 if (s[j] = '\n') and (s[j + 1] = 'n') then begin
  156.                                     delete(s, j, 1);
  157.                                     s[j] := chr(13)
  158.                                 end;
  159.                                 j := succ(j)
  160.                             end;
  161.                         end;
  162.                         Shortcuts^^[i] := s;
  163.                     end;
  164.                     SetShortcutsMenu;
  165.                     DisposDialog(shcd);
  166.                     shcd := nil;
  167.                     ChangedResource(Handle(shortcuts));
  168.                     WriteResource(Handle(shortcuts));
  169.                     CloseShortcuts := true;
  170.                     exit(CloseShortcuts)
  171.                 end;
  172.         CloseShortcuts := false
  173.     end;
  174.  
  175. procedure savefont;
  176.     begin
  177.         with defFont^^ do begin
  178.             fnt := MWdefaultFont;
  179.             siz := MWdefaultSize
  180.         end;
  181.         ChangedResource(Handle(defFont));
  182.         WriteResource(Handle(defFont));
  183.     end;
  184.  
  185. procedure InitIRCPreferences;
  186.     var
  187.         i, j: integer;
  188.         a: AppFile;
  189.         s: string;
  190.         p: Point;
  191.         t: SFTypeList;
  192.         r: SFReply;
  193.     begin
  194.         ValidPrefs := false;
  195.         shcd := nil;
  196. {$IFC DEBUG}
  197.         if SetVol(nil, 0) = 0 then begin
  198.             i := OpenResFile('ircleTest');
  199.             if ResError = 0 then
  200.                 exit(InitIRCPreferences);
  201.         end;
  202. {$ENDC}
  203.         CountAppFiles(i, j);
  204.         if j = 0 then begin
  205.             repeat
  206.                 s := NewFileName('Save Preferences as:');
  207.                 if s = '' then begin
  208.                     t[0] := 'Pref';
  209.                     SetPt(p, 80, 30);
  210.                     SFGetFile(p, '', nil, 1, t, nil, r);
  211.                     if not r.good then
  212.                         ApplExit;
  213.                     if SetVol(nil, r.vRefNum) <> 0 then
  214.                         cycle;
  215.                     i := OpenResFile(r.fName);
  216.                 end
  217.                 else begin
  218.                     if Create(s, 0, 'præC', 'Pref') <> 0 then
  219.                         cycle;
  220.                     CreateResFile(s);
  221.                     if ResError = 0 then
  222.                         i := OpenResFile(s)
  223.                 end;
  224.             until ResError = 0;
  225.         end
  226.         else begin
  227.             GetAppFiles(1, a);
  228.             if SetVol(nil, a.vrefnum) = 0 then
  229.                 i := OpenResFile(a.fname);     {this really should not fail }
  230.         end;
  231.     end;
  232.  
  233. function GetPrefs (request: boolean): boolean;
  234.     var
  235.         d: DialogPtr;
  236.         h: Handle;
  237.         i, j: integer;
  238.         r: Rect;
  239.         m: MenuHandle;
  240.         s: str255;
  241.     begin
  242.         if not ValidPrefs then begin
  243.             default := defaultHndl(GetResource('Pref', 256));
  244.             if default = nil then begin
  245.                 default := defaultHndl(NewClearHandle(sizeof(defaultType)));
  246.                 AddResource(Handle(default), 'Pref', 256, 'ircle Prefs');
  247.                 default^^.port := 6667
  248.             end;
  249.             if gethandlesize(handle(default)) < sizeof(defaultType) then begin
  250.                 sethandlesize(handle(default), sizeof(defaultType));
  251.                 for i := 1 to 4 do
  252.                     default^^.notify[i] := false
  253.             end;
  254.             shortcuts := shortcutsHndl(GetResource('Pref', 257));
  255.             if shortcuts = nil then begin
  256.                 shortcuts := shortcutsHndl(NewClearHandle(sizeof(ShortcutsType)));
  257.                 AddResource(Handle(shortcuts), 'Pref', 257, 'ircle Shortcuts');
  258.             end;
  259.             EnableItem(GetMHandle(M_SHCUTS), 0);
  260.             SetShortcutsMenu;
  261.             defFont := FSHndl(GetResource('Pref', 258));
  262.             if defFont = nil then begin
  263.                 defFont := FSHndl(NewClearHandle(sizeof(FSType)));
  264.                 with defFont^^ do begin
  265.                     fnt := monaco;
  266.                     siz := 9
  267.                 end;
  268.                 AddResource(Handle(defFont), 'Pref', 258, 'ircle Font');
  269.             end;
  270.             with defFont^^ do begin
  271.                 MWdefaultFont := fnt;
  272.                 MWdefaultSize := siz
  273.             end;
  274.             i := ApplTask(@CloseShortcuts, mouseMsg + inGoAway);
  275.             m := GetMHandle(M_FONT);
  276.             EnableItem(m, 0);
  277.             AddResMenu(m, 'FONT');
  278.             DrawMenuBar;
  279.             CheckItem(m, 1, (MWdefaultSize = 9));
  280.             CheckItem(m, 2, (MWdefaultSize = 10));
  281.             CheckItem(m, 3, (MWdefaultSize = 12));
  282.             CheckItem(m, 4, (MWdefaultSize = 14));
  283.             for i := 6 to CountMItems(m) do begin
  284.                 GetItem(m, i, s);
  285.                 GetFNum(s, j);
  286.                 CheckItem(m, i, (j = MWdefaultFont));
  287.             end;
  288.             ApplExitproc(@saveFont);
  289.         end;
  290.         if (default^^.server = '') or (default^^.nick = '') or (default^^.userLoginName = '') then
  291.             request := true;
  292.         if request then begin
  293.             d := GetNewDialog(D_PREFS, nil, WindowPtr(-1));
  294.             defTODia(d);
  295.             SelIText(d, 3, 0, 32767);
  296.             repeat
  297.                 ModalDialog(nil, i);
  298.                 if i = 2 then begin
  299.                     DisposDialog(d);
  300.                     GetPrefs := false;
  301.                     exit(GetPrefs)
  302.                 end;
  303.                 if (i >= 9) and (i <= 12) then begin
  304.                     GetDItem(d, i, j, h, r);
  305.                     default^^.notify[i - 8] := not default^^.notify[i - 8];
  306.                     SetCtlValue(ControlHandle(h), ord(default^^.notify[i - 8]));
  307.                 end;
  308.             until i = 1;
  309.             defFROMDia(d);
  310.             DisposDialog(d)
  311.         end;
  312.         GetPrefs := (default^^.server <> '') and (default^^.nick <> '') and (default^^.userLoginName <> '');
  313.         default^^.nick := copy(default^^.nick, 1, 9);
  314.         serverConn := default^^.server;
  315.         serverPort := default^^.port;
  316.         ChangedResource(Handle(default));
  317.         WriteResource(Handle(default));
  318.     end;
  319.  
  320. procedure GetShortcuts;
  321.     var
  322.         m: MenuHandle;
  323.         i, j: integer;
  324.         h: Handle;
  325.         r: Rect;
  326.     begin
  327.         if shcd = nil then begin
  328.             shcd := GetNewDialog(D_SHCUTS, nil, WindowPtr(-1));
  329.             m := GetMHandle(261);
  330.             for i := 1 to 10 do begin
  331.                 GetDItem(shcd, i, j, h, r);
  332.                 if Shortcuts^^[i] = ' ' then
  333.                     SetIText(h, '')
  334.                 else
  335.                     SetIText(h, Shortcuts^^[i]);
  336.             end;
  337.  
  338.         end
  339.         else
  340.             SelectWindow(shcd);
  341.     end;
  342.  
  343. end.